

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="参考资料《Java Web开发基础教程》 —— 李俊（主要）编著@[toc] 1. Servlet 简介 Servlet 的定义：Servlet是一种运行在Web服务端的Java应用程序，是由SUN公司提供的一种用于开发动态Web资源的技术。 Servlet 的作用：  收集来自网页表单&lt;form&gt;的用户输入 通过与数据库交互来操作数据 动态创建网页  Servlet 的本质：遵循规范">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaWeb _ 响应客户端的Servlet技术">
<meta property="og:url" content="http://example.com/2021/10/16/JavaWeb%20_%20%E5%93%8D%E5%BA%94%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84Servlet%E6%8A%80%E6%9C%AF/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="参考资料《Java Web开发基础教程》 —— 李俊（主要）编著@[toc] 1. Servlet 简介 Servlet 的定义：Servlet是一种运行在Web服务端的Java应用程序，是由SUN公司提供的一种用于开发动态Web资源的技术。 Servlet 的作用：  收集来自网页表单&lt;form&gt;的用户输入 通过与数据库交互来操作数据 动态创建网页  Servlet 的本质：遵循规范">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2274d8cbf398451084ba9c8ecd5cf67d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/288ce97c0d5b4439aa3d9f762e80bc0b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0c9b5e448b984f9399c69d038705cb31.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4be18cd5a271461396bc88d332f3b7b3.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/07db5c3baea64ad2affac39ecc4645d6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6cef4a594f824e158ab69a78f26e3976.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0ca4de14eb384487bc8304074468e97c.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b6ad52082be54af4973a5cc5d8eacff5.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7846924d151442b7ac12726ee6c0e162.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2deb836aa15f4311903a3fe354e94747.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/f015e46d02144ab3bf1d8eff69c7c2b9.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/dea5bb9b76c94a10aaf248235d5b1ff6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/51f7ffefa25b4954b898e4e974e959e1.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a7849d97927e45cb83cb9ee333e31414.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9b08e31d6bfe4e41b95ddc469845ae01.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/084887a664fa4b619ac1d4e9b058c0de.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6b148850b6644ec5bf1475fc134d9729.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e3086ca8783c4a04a5d435836406fe4c.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6243d6d421a5487bb643aafbaf408c69.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/5082d40ab329436cb1425af7ffb967f6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7ab876f271474e7baee4e87207fcbf09.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/afa8018b673f426b92d9569f4b2753e8.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/35ef81dcb9cb4a84ac38d149c00d3130.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/51ca1b679dda4cc5afd8d208d1fee615.png">
<meta property="article:published_time" content="2021-10-16T07:44:55.000Z">
<meta property="article:modified_time" content="2022-08-22T15:45:41.398Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="JavaWeb">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/2274d8cbf398451084ba9c8ecd5cf67d.png">
  
  
  
  <title>JavaWeb _ 响应客户端的Servlet技术 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="JavaWeb _ 响应客户端的Servlet技术"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-10-16 15:44" pubdate>
          2021年10月16日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          12k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          100 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JavaWeb _ 响应客户端的Servlet技术</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p>《Java Web开发基础教程》 —— 李俊（主要）编著<br>@[toc]</p>
<h1 id="1-Servlet-简介"><a href="#1-Servlet-简介" class="headerlink" title="1. Servlet 简介"></a>1. Servlet 简介</h1><hr>
<p>Servlet 的定义：Servlet是一种运行在Web服务端的<code>Java</code>应用程序，是由SUN公司提供的一种用于开发<code>动态</code>Web资源的技术。</p>
<p>Servlet 的作用：</p>
<ul>
<li>收集来自网页<code>表单&lt;form&gt;</code>的用户输入</li>
<li>通过与数据库交互来操作数据</li>
<li><code>动态</code>创建网页</li>
</ul>
<p>Servlet 的本质：遵循规范进行编写的Java类，没有main方法，创建、使用、销毁都有Servlet<code>容器</code>进行管理</p>
<p><img src="https://img-blog.csdnimg.cn/2274d8cbf398451084ba9c8ecd5cf67d.png" srcset="/img/loading.gif" lazyload alt="Servlet在JavaWeb应用中的位置"></p>
<p>如上图所示，将HTTP请求交由给Servlet程序处理的HTTP服务器通常称为Servlet<code>容器</code>，常见容器有<code>Tomcat</code>、Jetty、WebLogic以JBoss等</p>
<h1 id="2-开发环境搭建"><a href="#2-开发环境搭建" class="headerlink" title="2. 开发环境搭建"></a>2. 开发环境搭建</h1><hr>
<h2 id="2-1-安装配置Tomcat"><a href="#2-1-安装配置Tomcat" class="headerlink" title="2.1 安装配置Tomcat"></a>2.1 安装配置Tomcat</h2><ul>
<li>Windows10 操作系统</li>
<li>JDK8——用于支持基于Java语言开发的软件环境 </li>
<li>Tomcat9——基于Java语言开发的免费开源的Web应用服务器，即Servlet容器</li>
<li>IDEA 2021 专业版——Java语言开发的集成环境，可搭建JavaWeb项目（Community Edition社区版除外）</li>
</ul>
<p>这里主要介绍Tomcat的安装与配置</p>
<p>Tomact官网 <a target="_blank" rel="noopener" href="https://tomcat.apache.org/">https://tomcat.apache.org/</a></p>
<p>以 【Tomcat 9.0.52 Released】版本为例<br><img src="https://img-blog.csdnimg.cn/288ce97c0d5b4439aa3d9f762e80bc0b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>选择可执行文件，方便安装</p>
<p><img src="https://img-blog.csdnimg.cn/0c9b5e448b984f9399c69d038705cb31.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>安装后的目录结构</p>
<p><img src="https://img-blog.csdnimg.cn/4be18cd5a271461396bc88d332f3b7b3.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>配置环境变量，在系统变量一栏新建，用于表示JDK和Tomcat的目录</p>
<p><img src="https://img-blog.csdnimg.cn/07db5c3baea64ad2affac39ecc4645d6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>在系统变量中的PATH变量添加JDK和Tomcat的bin文件夹<img src="https://img-blog.csdnimg.cn/6cef4a594f824e158ab69a78f26e3976.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>至此，Tomcat配置完毕，接下来是web项目的创建。</p>
<h2 id="2-2-IDEA创建Web项目"><a href="#2-2-IDEA创建Web项目" class="headerlink" title="2.2 IDEA创建Web项目"></a>2.2 IDEA创建Web项目</h2><p>首次创建Java企业项目，需要新建Tomcat的服务器<br><img src="https://img-blog.csdnimg.cn/0ca4de14eb384487bc8304074468e97c.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/b6ad52082be54af4973a5cc5d8eacff5.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>找到之前安装好的Tomcat目录<br><img src="https://img-blog.csdnimg.cn/7846924d151442b7ac12726ee6c0e162.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>若下方未显示JDK，点击添加后，选择对应的JDK目录即可</p>
<p><img src="https://img-blog.csdnimg.cn/2deb836aa15f4311903a3fe354e94747.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>点击下一步后，只需选择<code>Servlet</code><br><img src="https://img-blog.csdnimg.cn/f015e46d02144ab3bf1d8eff69c7c2b9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>目前的项目结构</p>
<p><img src="https://img-blog.csdnimg.cn/dea5bb9b76c94a10aaf248235d5b1ff6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="3-Hello-Servlet"><a href="#3-Hello-Servlet" class="headerlink" title="3. Hello Servlet"></a>3. Hello Servlet</h1><hr>
<h2 id="3-1-编写测试类"><a href="#3-1-编写测试类" class="headerlink" title="3.1 编写测试类"></a>3.1 编写测试类</h2><p><code>HelloServlet.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example.project;<br><br><span class="hljs-keyword">import</span> java.io.*;<br><span class="hljs-keyword">import</span> javax.servlet.http.*;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.*;<br><br><span class="hljs-meta">@WebServlet(name = &quot;helloServlet&quot;, value = &quot;/hello&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doGet</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        System.out.println(<span class="hljs-string">&quot;Hello,Servlet!&quot;</span>);<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="3-2-编写跳转页面"><a href="#3-2-编写跳转页面" class="headerlink" title="3.2 编写跳转页面"></a>3.2 编写跳转页面</h2><p><code>index.jsp</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs html">&lt;%@ page contentType=&quot;text/html; charset=UTF-8&quot; pageEncoding=&quot;UTF-8&quot; %&gt;<br><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JSP - Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>&lt;%= &quot;Hello World!&quot; %&gt;<br><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;hello&quot;</span>&gt;</span>Hello Servlet<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/51f7ffefa25b4954b898e4e974e959e1.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>访问<code>localhost:8080/project_war_exploded</code>的页面</p>
<p><img src="https://img-blog.csdnimg.cn/a7849d97927e45cb83cb9ee333e31414.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>点击超链接后的页面<br><img src="https://img-blog.csdnimg.cn/9b08e31d6bfe4e41b95ddc469845ae01.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="3-3-修改Tomcat配置"><a href="#3-3-修改Tomcat配置" class="headerlink" title="3.3 修改Tomcat配置"></a>3.3 修改Tomcat配置</h2><p>通过刚才的测试可以发现，访问的URL末尾有项目的名称project_war_exploded，那么如何省去这个名称，使得测试URL看起来更简洁呢，接下来就需要配置一下Tomcat<br><img src="https://img-blog.csdnimg.cn/084887a664fa4b619ac1d4e9b058c0de.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>首先设置自动更新类和资源，这样在修改JSP页面的时候，服务器可以自动的刷新<br><img src="https://img-blog.csdnimg.cn/6b148850b6644ec5bf1475fc134d9729.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>接着点击部署，然后向下滑动滚动条<br><img src="https://img-blog.csdnimg.cn/e3086ca8783c4a04a5d435836406fe4c.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>选择第一个空白<br><img src="https://img-blog.csdnimg.cn/6243d6d421a5487bb643aafbaf408c69.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>选择完毕后依次点击应用Apply和确定OK<br>再次启动Tomcat后，效果如下<br><img src="https://img-blog.csdnimg.cn/5082d40ab329436cb1425af7ffb967f6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="4-Servlet-标识的两种方式"><a href="#4-Servlet-标识的两种方式" class="headerlink" title="4.Servlet 标识的两种方式"></a>4.Servlet 标识的两种方式</h1><hr>
<p>对于部分3的测试，你是否疑惑，为什么在点击超链接后，web服务器能执行java类的代码呢？<br>在了解这些之前，我们看看前端页面的关键一句代码：<code>&lt;a href=&quot;hello&quot;&gt;Hello Servlet&lt;/a&gt;</code><br>当我们在页面点击超链接后，地址URL变成了<code>localhost:8080/hello</code>，同时也可以直接访问这个地址。</p>
<p>在helloServlet类中有这么一句：<code>@WebServlet(name = &quot;helloServlet&quot;, value = &quot;/hello&quot;)</code><br>在Java当中像这样带@符号的叫做<code>注解</code>，其作用是将当前类标识为一个Servlet类，这样Tomcat<br>容器便可以访问到该类。</p>
<p><code>注：Servlet标识不能重复，否则部署项目时会报错</code></p>
<h2 id="4-1-注解方式"><a href="#4-1-注解方式" class="headerlink" title="4.1 注解方式"></a>4.1 注解方式</h2><p>有两种方式使用注解来标识Servlet类</p>
<ul>
<li><code>@WebServlet(name = &quot;ServletClassName&quot;, value = &quot;url&quot;)</code></li>
<li><code>@WebServlet(name = &quot;ServletClassName&quot;, urlPattern=&quot;url&quot;) </code></li>
</ul>
<p>其中的url便是前端网页调用时需要访问的地址比如 @WebServlet(name &#x3D; “helloServlet”, value &#x3D; “&#x2F;hello”)</p>
<h2 id="4-2-XML配置文件方式"><a href="#4-2-XML配置文件方式" class="headerlink" title="4.2 XML配置文件方式"></a>4.2 XML配置文件方式</h2><p>在web项目中通常会有web.xml配置文件<br><img src="https://img-blog.csdnimg.cn/7ab876f271474e7baee4e87207fcbf09.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>注解是JDK1.5版本、Servlet3.0版本之后开始出现的，在那之前，JavaWeb项目里的Servlet声明通常是通过配置该文件，比如声明之前的hello类，具体代码如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">version</span>=<span class="hljs-string">&quot;4.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>hello<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>com.example.project.HelloServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span><br>    <br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>hello<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/hello<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span><br><br></code></pre></td></tr></table></figure>
<p>XML配置Servlet说明</p>
<ul>
<li><code>&lt;servlet&gt;</code> 用于注册Servlet，并且需要制定Servlet名称和完整类名</li>
<li><code>&lt;serlvet-mapping&gt;</code>则用于映射Servlet对外访问的虚拟路径</li>
<li><code>&lt;servlet-name&gt;</code>必须存在于某个<code>&lt;serlvet&gt;</code>中</li>
<li><code>&lt;url-parrtern&gt;</code> 则用于指定访问该Servlet的虚拟路径，以”&#x2F;“开头则表示Web应用程序的根目录</li>
</ul>
<h1 id="5-Servlet-的生命周期"><a href="#5-Servlet-的生命周期" class="headerlink" title="5. Servlet 的生命周期"></a>5. Servlet 的生命周期</h1><hr>
<p>Servlet和Java中其他任何对象一样，都有生命周期，即对象从创建到销毁的过程。</p>
<p><img src="https://img-blog.csdnimg.cn/afa8018b673f426b92d9569f4b2753e8.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="5-1-初始化阶段"><a href="#5-1-初始化阶段" class="headerlink" title="5.1 初始化阶段"></a>5.1 初始化阶段</h2><p>可以在Servlet类中重写init()方法，来编写Servlet类初始化的代码</p>
<p>当Servlet 容器（Tomcat）接收到客户端发来的<code>HTTP请求</code>要访问某个Servlet时，首先检查内存中是否已经有了该Servlet对象，如果有就直接使用，否则就创建Servlet实例对象。</p>
<p>调用 init()方法实现Servlet的初始化工作，在整个生命周期过程中只执行一次。</p>
<p>在代码具体实现中有两种重写方式：</p>
<ul>
<li><code>public void init(ServletConfig config)</code> 该方法用到了Servlet的config对象，这是读取关于Servlet的一些配置，有XML配置和注解配置两种方式，在之后部分会详细介绍</li>
<li><code>public void init()</code> 仅仅做初始化的工作，具体工作由开发者编写</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example.project;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletConfig;<br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.http.*;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.*;<br><br><span class="hljs-meta">@WebServlet(name = &quot;helloServlet&quot;, value = &quot;/hello&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">(ServletConfig config)</span> <span class="hljs-keyword">throws</span> ServletException &#123;<br>        <span class="hljs-built_in">super</span>.init(config);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> ServletException &#123;<br>        <span class="hljs-built_in">super</span>.init();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="5-2-运行阶段"><a href="#5-2-运行阶段" class="headerlink" title="5.2 运行阶段"></a>5.2 运行阶段</h2><p>Servlet容器（Tomcat）将HTTP<code>请求和响应</code>封装成<code>Java对象</code> 并作为参数传给<code>service()方法</code>，该方法会根据HTTP请求的类型决定调用的<code>doGet()</code>或<code>doPost()</code>方法来处理业务逻辑。</p>
<p>Servlet容器对于每一次的访问请求都会调用一次<code>service()</code>方法，而在实际开发中，一般只关注<code>doGet()</code>和<code>doPost()</code>方法中具体业务的实现。</p>
<h2 id="5-3-销毁阶段"><a href="#5-3-销毁阶段" class="headerlink" title="5.3 销毁阶段"></a>5.3 销毁阶段</h2><p>当服务器关闭或Web应用被移除Servlet容器时，Servlet对象被销毁。</p>
<p>销毁之前，容器会调用<code>destroy()</code>方法，以便Servlet对象释放所占用的资源。</p>
<h1 id="6-Servlet-的常用对象"><a href="#6-Servlet-的常用对象" class="headerlink" title="6. Servlet 的常用对象"></a>6. Servlet 的常用对象</h1><hr>
<p>Servlet的具体实现是通过继承HttpServlet类来进行的，作为一个Java类，它包含一些常用对象：</p>
<table>
<thead>
<tr>
<th align="left">对象名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">ServletConfig</td>
<td align="left">初始化Servlet</td>
</tr>
<tr>
<td align="left">ServletContext</td>
<td align="left">共享多个Servlet的信息</td>
</tr>
<tr>
<td align="left">HttpServletRequest</td>
<td align="left">HTTP请求信息</td>
</tr>
<tr>
<td align="left">HttpServletResponse</td>
<td align="left">HTTP响应信息</td>
</tr>
</tbody></table>
<h2 id="6-1-ServletConfig-对象"><a href="#6-1-ServletConfig-对象" class="headerlink" title="6.1 ServletConfig 对象"></a>6.1 ServletConfig 对象</h2><hr>
<h3 id="6-1-1-方法说明"><a href="#6-1-1-方法说明" class="headerlink" title="6.1.1 方法说明"></a>6.1.1 方法说明</h3><p>该对象主要是用于存储一些关于Servlet的配置信息，常用方法有：</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">String getInitParameter(String name)</td>
<td align="left">根据初始化参数名返回对应的参数</td>
</tr>
<tr>
<td align="left">Enumeration getInitParameterNames()</td>
<td align="left">返回所有的初始化参数名</td>
</tr>
<tr>
<td align="left">ServletContext getServletContext()</td>
<td align="left">返回一个表示当前Web应用的ServletContext对象</td>
</tr>
<tr>
<td align="left">String getServletNames()</td>
<td align="left">返回 Servlet 的名字</td>
</tr>
</tbody></table>
<h3 id="6-1-2-测试"><a href="#6-1-2-测试" class="headerlink" title="6.1.2 测试"></a>6.1.2 测试</h3><ul>
<li><p>注解方式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example.project;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletConfig;<br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.http.*;<br><span class="hljs-keyword">import</span> javax.servlet.annotation.*;<br><br><span class="hljs-meta">@WebServlet(name = &quot;helloServlet&quot;,</span><br><span class="hljs-meta">        value = &quot;/hello&quot;,</span><br><span class="hljs-meta">        initParams = &#123;</span><br><span class="hljs-meta">        @WebInitParam(name = &quot;Encoding&quot;, value = &quot;UTF-8&quot;),</span><br><span class="hljs-meta">                @WebInitParam(name = &quot;myName&quot;, value = &quot;lazycat&quot;)</span><br><span class="hljs-meta">        &#125;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">(ServletConfig config)</span> <span class="hljs-keyword">throws</span> ServletException &#123;<br>        System.out.println(config.getInitParameter(<span class="hljs-string">&quot;Encoding&quot;</span>));<br>        System.out.println(<span class="hljs-string">&quot;Hello &quot;</span> + config.getInitParameter(<span class="hljs-string">&quot;myName&quot;</span>));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>效果图：<br><img src="https://img-blog.csdnimg.cn/35ef81dcb9cb4a84ac38d149c00d3130.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
</li>
<li><p>XML方式</p>
</li>
</ul>
<p><code>web.xml</code></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">version</span>=<span class="hljs-string">&quot;4.0&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>hello<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>com.example.project.HelloServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>Encoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>            <br>            <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>myName<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>lazycat<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span><br>    <br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>hello<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/hello<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>这种方式下的Servlet类简化为：<br><code>HelloServlet.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.example.project;<br><br><span class="hljs-keyword">import</span> javax.servlet.ServletConfig;<br><span class="hljs-keyword">import</span> javax.servlet.ServletException;<br><span class="hljs-keyword">import</span> javax.servlet.http.*;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">HelloServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">HttpServlet</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">init</span><span class="hljs-params">(ServletConfig config)</span> <span class="hljs-keyword">throws</span> ServletException &#123;<br>        System.out.println(config.getInitParameter(<span class="hljs-string">&quot;Encoding&quot;</span>));<br>        System.out.println(<span class="hljs-string">&quot;Hello &quot;</span> + config.getInitParameter(<span class="hljs-string">&quot;myName&quot;</span>));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>通过比较发现，还是注解看起来简洁一些。</p>
<h2 id="6-2-ServletContext对象"><a href="#6-2-ServletContext对象" class="headerlink" title="6.2 ServletContext对象"></a>6.2 ServletContext对象</h2><hr>
<p>Servlet容器（Tomcat）启动时，会为每个Web应用创建一个<code>唯一</code>的<code>ServletContext</code>对象来代表当前Web应用。</p>
<p>该对象不仅封装了当前Web应用的所有信息，而且实现了多个Servlet之间数据的共享。</p>
<p>它的主要作用有：获取Web初始化参数、实现多个Servlet的数据共享</p>
<p>常用方法有：</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Enumeration getAtrributeNames()</td>
<td align="left">返回所有ServletContext所有域属性名的一个可迭代对象</td>
</tr>
<tr>
<td align="left">Object getAttribute(String name)</td>
<td align="left">根据参数属性名返回相应属属性值</td>
</tr>
<tr>
<td align="left">void removeAttribute(String name)</td>
<td align="left">根据参数属性名删除相应的域属性</td>
</tr>
<tr>
<td align="left">void setAttribute(String name, Object obj)</td>
<td align="left">设置域参数，其中name是属性名，obj是属性值</td>
</tr>
</tbody></table>
<h3 id="6-2-1-获取Web应用的初始化参数"><a href="#6-2-1-获取Web应用的初始化参数" class="headerlink" title="6.2.1 获取Web应用的初始化参数"></a>6.2.1 获取Web应用的初始化参数</h3><p>在<code>web.xml</code>文件中，除了配置Servlet的初始化信息外，还可以配置整个Web应用的初始化信息</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">context-param</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>appName<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>javaweb<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">context-param</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">context-param</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>appAddress<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>myapp<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">context-param</span>&gt;</span><br><br></code></pre></td></tr></table></figure>
<p><code>&lt;context-param&gt;</code> 位于 <code>&lt;web-app&gt;</code>中</p>
<p>在Servlet类中获取所有初始化参数的值.以doGet方法为例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">ServletContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.getServletContext();<br>Enumeration&lt;String&gt; names = context.getInitParameterNames();<br><span class="hljs-keyword">while</span>(names.hasMoreElements()&#123;<br>	<span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> names.nextElement();<br>	<span class="hljs-type">String</span> <span class="hljs-variable">value</span> <span class="hljs-operator">=</span> context.getInitParameter(name);<br>	System.out.println(name + <span class="hljs-string">&quot;,&quot;</span> + value);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="6-2-2-实现多个Servlet的数据共享"><a href="#6-2-2-实现多个Servlet的数据共享" class="headerlink" title="6.2.2 实现多个Servlet的数据共享"></a>6.2.2 实现多个Servlet的数据共享</h3><p>以下两个Servlet类均以doGet方法为例<br><code>servlet1.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-built_in">this</span>.getServletContext().setAttribute(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;test&quot;</span>);<br></code></pre></td></tr></table></figure>

<p><code>servlet2.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">ServletContext</span> <span class="hljs-variable">sc</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.getServletContext();<br>System.out.println(sc.getAttribute(<span class="hljs-string">&quot;data&quot;</span>));<br></code></pre></td></tr></table></figure>

<h2 id="6-3-HttpServletRequest-对象"><a href="#6-3-HttpServletRequest-对象" class="headerlink" title="6.3 HttpServletRequest 对象"></a>6.3 HttpServletRequest 对象</h2><hr>
<p>该对象是doGet和doPost方法的第一个参数。</p>
<p>HTTP请求消息分为<code>请求消息头</code> 和 <code>请求消息体</code> 两部分。</p>
<h3 id="6-3-1-获取请求消息头"><a href="#6-3-1-获取请求消息头" class="headerlink" title="6.3.1 获取请求消息头"></a>6.3.1 获取请求消息头</h3><p>相关方法：</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">String getMethod()</td>
<td align="left">获取HTTP的请求方式（如GET，POST等）</td>
</tr>
<tr>
<td align="left">String getRequestURI()</td>
<td align="left">获取URL请求的资源名称部分，即位于URL的主机和端口号之后，参数之前的部分</td>
</tr>
<tr>
<td align="left">String getContextPath()</td>
<td align="left">获取URL请求的参数部分，即URL中间号（?）后面的所有内容</td>
</tr>
<tr>
<td align="left">String getServletPath()</td>
<td align="left">获取URL中Web应用程序的路径，以”&#x2F;“开头</td>
</tr>
<tr>
<td align="left">Enumeration getHeaderNames()</td>
<td align="left">获取所有请求消息头的名称</td>
</tr>
<tr>
<td align="left">String getHeader(String name)</td>
<td align="left">获取一个指定名称的头字段的值</td>
</tr>
<tr>
<td align="left">String getCharcterEncoding()</td>
<td align="left">获取请求消息体部分的字符集编码</td>
</tr>
</tbody></table>
<h3 id="6-3-2-获取请求消息体"><a href="#6-3-2-获取请求消息体" class="headerlink" title="6.3.2 获取请求消息体"></a>6.3.2 获取请求消息体</h3><p>相关方法：</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">String getParameter(String name)</td>
<td align="left">获取某个指定名称的参数值，若不存在则返回null</td>
</tr>
<tr>
<td align="left">String[] getParameterValues(String name)</td>
<td align="left">Http请求消息中可有相同名称的参数，（如表单的复选框），返回所有对应参数值</td>
</tr>
</tbody></table>
<h3 id="6-3-3-解决乱码-amp-请求转发-Servlet-传递"><a href="#6-3-3-解决乱码-amp-请求转发-Servlet-传递" class="headerlink" title="6.3.3 解决乱码 &amp; 请求转发 Servlet 传递"></a>6.3.3 解决乱码 &amp; 请求转发 Servlet 传递</h3><p>解决乱码 ：</p>
<ul>
<li>doPost方法<code>request.setCharacterEncoding(&quot;utf-8&quot;);</code></li>
<li>doGet方法<br> <code>String data = req.getParameter(&quot;data&quot;)</code><br><code>data = new String(data.getBytes(&quot;iso-8859-1&quot;), &quot;utf-8&quot;);</code></li>
</ul>
<p>请求转发：<br>在Servlet中，当一个Web资源收到客户端请求后，如果希望Servlet处理请求，或希望将请求的数据转发到页面上显示，可通过<code>RequestDispathcer 对象</code>的 <code>forward()</code>方法实现。</p>
<p>ServletRequest接口定义了 <code>getRequestDispatcher(String path)</code> 方法</p>
<p>作用：返回封装某个路径所指定资源的RequestDispatcher对象，其中<code>path</code>必须以”&#x2F;“开头，表示当前Web应用的根目录</p>
<p><code>注：WEB-INF目录的内容对 RequestDispathcer对象 可见</code></p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">forward(ServletRequest request, ServletResponse response</td>
<td align="left">从一个Servlet传递给另一个Web资源。该方法必须在响应提交给客户端之前被调用</td>
</tr>
</tbody></table>
<p>范例：servlet1 请求传递到servlet2（以doGet方法为例）<br><code>servlet1.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java">request.setAttribute(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;lazycat&quot;</span>);<br>request.getRequestDispathcer(<span class="hljs-string">&quot;/servlet2&quot;</span>).forward(request, response);<br></code></pre></td></tr></table></figure>
<p><code>servlet2.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">name</span> <span class="hljs-operator">=</span> (String)request.getAttribute(<span class="hljs-string">&quot;name&quot;</span>);<br></code></pre></td></tr></table></figure>

<h3 id="6-3-4-请求转发的5个特点"><a href="#6-3-4-请求转发的5个特点" class="headerlink" title="6.3.4 请求转发的5个特点"></a>6.3.4 请求转发的5个特点</h3><ul>
<li>浏览器地址栏无变化，请求转发属于服务器端内部的行为，作为客户端并不能知道该过程</li>
<li>不管服务器端请求转发多少次，对于客户端都属于一次请求。</li>
<li>转发的Servlet之间共享request域对象的数据</li>
<li>可转发到<code>WEB-INF</code>目录下的页面文件中</li>
<li>不可以访问工程以外的资源。该过程只能发生在同一个工程的不同Web资源之间</li>
</ul>
<h2 id="6-4-HttpServletResponse-对象"><a href="#6-4-HttpServletResponse-对象" class="headerlink" title="6.4 HttpServletResponse 对象"></a>6.4 HttpServletResponse 对象</h2><hr>
<p>HTTP响应消息分为<code>状态行</code>、<code>响应头</code>和<code>消息体</code>三部分</p>
<h3 id="6-4-1-常用设置响应消息头字段方法"><a href="#6-4-1-常用设置响应消息头字段方法" class="headerlink" title="6.4.1 常用设置响应消息头字段方法"></a>6.4.1 常用设置响应消息头字段方法</h3><table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">void setHeader(String name, String value)</td>
<td align="left">设置HTTP的响应头字段</td>
</tr>
<tr>
<td align="left">void setContentLength(int len)</td>
<td align="left">设置响应消息的实体内容大小，单位为字节（B）</td>
</tr>
<tr>
<td align="left">void setContextType(String type)</td>
<td align="left">设置Servlet输出内容的MIME类型。例如，若发送到客户端的内容是PDF的文档数据，则设置为”application&#x2F;pdf”，若为文本，则为”text&#x2F;html;charset&#x3D;UTF-8”</td>
</tr>
<tr>
<td align="left">void setCharacterEncoding(String charset)</td>
<td align="left">设置输出内容使用的字符编码</td>
</tr>
</tbody></table>
<h3 id="6-4-2-response-对象输出流的方法"><a href="#6-4-2-response-对象输出流的方法" class="headerlink" title="6.4.2 response 对象输出流的方法"></a>6.4.2 response 对象输出流的方法</h3><table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">getOutputStream()</td>
<td align="left">获取字节输出流对象，可直接输出字节数组中的二进制数据</td>
</tr>
<tr>
<td align="left">getWriter()</td>
<td align="left">获取类型为PrintWriter的字节输出流对象，可直接输出字符文本内容</td>
</tr>
</tbody></table>
<h3 id="6-4-3-文件下载-amp-文本输出"><a href="#6-4-3-文件下载-amp-文本输出" class="headerlink" title="6.4.3 文件下载 &amp; 文本输出"></a>6.4.3 文件下载 &amp; 文本输出</h3><p>通过该对象可实现文件下载，以doGet方法实现下载PDF文件为例（pdf在index.jsp同级目录中）：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java">response.setContextType(<span class="hljs-string">&quot;application/pdf&quot;</span>);<br>response.setHeader(<span class="hljs-string">&quot;Context-Disposition&quot;</span>, <span class="hljs-string">&quot;attachment;fileName=&quot;</span>+<span class="hljs-string">&quot;save.pdf&quot;</span>);<br><span class="hljs-type">String</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">this</span>.getServletContext().getRealPath(<span class="hljs-string">&quot;/&quot;</span>);<br><span class="hljs-type">File</span> <span class="hljs-variable">file</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">File</span>(path + <span class="hljs-string">&quot;test.pdf&quot;</span>);<br>ServletOutputStream out;<br><span class="hljs-keyword">try</span>&#123;<br>	<span class="hljs-type">FileInputStream</span> <span class="hljs-variable">inputStream</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FileInputStream</span>(file);<br>	out = response.getOutputStream();<br>	<span class="hljs-type">int</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>	<span class="hljs-type">byte</span>[] buffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">512</span>];<br>	<span class="hljs-keyword">while</span>((b = inputStream.read(buffer)) != -<span class="hljs-number">1</span>)&#123;<br>		out.wirte(buffer, <span class="hljs-number">0</span>, b);<br>	&#125;<br>	inputStream.close();<br>	out.close();<br>	out.flush();	<span class="hljs-comment">// 强制刷新缓冲区</span><br>&#125; <span class="hljs-keyword">catch</span>(IOException e)&#123;<br>	e.printStackTrace();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>文本输出</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">data</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;test print&quot;</span>;<br><span class="hljs-type">PrintWriter</span> <span class="hljs-variable">out</span> <span class="hljs-operator">=</span> response.getWriter();<br>out.write(data);<br></code></pre></td></tr></table></figure>
<h3 id="6-4-4-解决乱码-amp-请求重定向"><a href="#6-4-4-解决乱码-amp-请求重定向" class="headerlink" title="6.4.4 解决乱码 &amp; 请求重定向"></a>6.4.4 解决乱码 &amp; 请求重定向</h3><p>解决乱码：<br>方式一：<br><code>response.setCharacterEncoding(&quot;UTF-8&quot;);</code><br><code>response.setHeader(&quot;Context-type&quot;, &quot;text/html;charset = utf-8&quot;);</code></p>
<p>方式二：<br><code>response.setContextType(&quot;text/html;charset=&quot;utf-8&quot;)</code></p>
<p>请求重定向：指当Web服务器端接收到客户端的请求后，可能由于某些原因不能访问当前请求的URL所指向的Wweb资源，或服务器根据业务需求而重新指定了一个新的资源路径，让客户端重发请求</p>
<p>可是使用<code>sendRedirect(String location) throws IOException</code> 实现请求重定向</p>
<p><code>location</code>用于指定重新发送请求的URL地址（相对或者绝对地址）</p>
<p>原理图如下：<br><img src="https://img-blog.csdnimg.cn/51ca1b679dda4cc5afd8d208d1fee615.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="6-4-5-请求重定向的5个特点"><a href="#6-4-5-请求重定向的5个特点" class="headerlink" title="6.4.5 请求重定向的5个特点"></a>6.4.5 请求重定向的5个特点</h3><ul>
<li>浏览器地址栏会发生变化，请求重定向会告知客户端浏览器重新定位的URL地址，浏览器则会根据该地址重新发送请求</li>
<li>客户端发送了两次请求。客户端第一次请求资源后，根据<code>sendRedirect()</code>方法返回的URL路径，重新发送第二次请求</li>
<li>不共享request域对象中的数据。两次请求的数据无法共享</li>
<li>不能访问WEB-INF下的资源</li>
<li>可访问当前工程以外的资源</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/JavaEE/" class="category-chain-item">JavaEE</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaWeb/">#JavaWeb</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>JavaWeb _ 响应客户端的Servlet技术</div>
      <div>http://example.com/2021/10/16/JavaWeb _ 响应客户端的Servlet技术/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年10月16日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/10/19/Android%20_%20%E7%AC%AC1%E7%AB%A0%20_%20UI%E8%AE%BE%E8%AE%A1%20_%20%E7%9F%A5%E8%AF%86%E7%82%B9%E6%B1%87%E6%80%BB%20_%20+Demo/" title="Android _ 第1章 _ UI设计 _ 知识点汇总 _ +Demo">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Android _ 第1章 _ UI设计 _ 知识点汇总 _ +Demo</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/13/JavaWeb%20_%20%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%99%BB%E9%99%86%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%20_%20Servlet%20%E5%92%8C%20JQuery%20Ajax/" title="JavaWeb _ 实现简单登陆的两种方式 _ Servlet 和 JQuery Ajax">
                        <span class="hidden-mobile">JavaWeb _ 实现简单登陆的两种方式 _ Servlet 和 JQuery Ajax</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
